import { DataTypes, Model } from 'sequelize'
import { sequelize } from './index'

export class Game extends Model {
  public id!: string
  public roomId!: number
  public type!: string
  public status!: string
  public config!: any
  public result?: string
  public startTime!: Date
  public endTime?: Date
}

Game.init(
  {
    id: {
      type: DataTypes.STRING(32),
      primaryKey: true,
    },
    roomId: {
      type: DataTypes.INTEGER,
      allowNull: false,
    },
    type: {
      type: DataTypes.STRING(20),
      allowNull: false,
    },
    status: {
      type: DataTypes.ENUM('waiting', 'betting', 'drawing', 'finished'),
      defaultValue: 'waiting',
    },
    config: {
      type: DataTypes.JSON,
      allowNull: false,
    },
    result: {
      type: DataTypes.STRING(50),
    },
    startTime: {
      type: DataTypes.DATE,
      allowNull: false,
    },
    endTime: {
      type: DataTypes.DATE,
    },
  },
  {
    sequelize,
    tableName: 'games',
    indexes: [
      {
        fields: ['roomId'],
      },
      {
        fields: ['startTime'],
      },
    ],
  }
)
